﻿namespace UnityEditor
{
    using System;
    using System.Collections.Generic;
    using System.Runtime.CompilerServices;
    using UnityEditorInternal;
    using UnityEngine;

    internal class UploadingBuildsMonitor : ScriptableObject
    {
        private bool m_DidInit;
        private Dictionary<string, ValueSmoother> m_ProgressSmoothers = new Dictionary<string, ValueSmoother>();
        private UploadingBuild[] m_UploadingBuilds = new UploadingBuild[0];
        private static Content s_Content;
        private static UploadingBuildsMonitor s_UploadingBuildsMonitor;

        private UploadingBuildsMonitor()
        {
            if (s_Content == null)
            {
                s_Content = new Content();
            }
        }

        public static void Activate()
        {
            if (s_UploadingBuildsMonitor == null)
            {
                ScriptableObject.CreateInstance(typeof(UploadingBuildsMonitor));
            }
        }

        public static void Deactivate()
        {
            UnityEngine.Object.DestroyImmediate(s_UploadingBuildsMonitor);
        }

        public static string GetActiveSessionID()
        {
            return (!string.IsNullOrEmpty(AssetStoreClient.ActiveSessionID) ? (AssetStoreClient.ActiveSessionID + InternalEditorUtility.GetAuthToken()) : string.Empty);
        }

        private ValueSmoother GetProgressSmoother(string url)
        {
            ValueSmoother smoother;
            this.m_ProgressSmoothers.TryGetValue(url, out smoother);
            if (smoother == null)
            {
                smoother = new ValueSmoother();
                this.m_ProgressSmoothers[url] = smoother;
            }
            return smoother;
        }

        public static void HandleNoSession(string displayName)
        {
            <HandleNoSession>c__AnonStorey46 storey = new <HandleNoSession>c__AnonStorey46 {
                displayName = displayName
            };
            Activate();
            AssetStoreLoginWindow.Login(s_Content.m_NoSessionDialogText.text, new AssetStoreLoginWindow.LoginCallback(storey.<>m__97));
        }

        public static void InternalStateChanged()
        {
            if (s_UploadingBuildsMonitor != null)
            {
                s_UploadingBuildsMonitor.SyncToState();
            }
        }

        private void OnDisable()
        {
            s_UploadingBuildsMonitor = null;
            EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Remove(EditorApplication.update, new EditorApplication.CallbackFunction(this.Update));
        }

        private void OnEnable()
        {
            s_UploadingBuildsMonitor = this;
            EditorApplication.update = (EditorApplication.CallbackFunction) Delegate.Combine(EditorApplication.update, new EditorApplication.CallbackFunction(this.Update));
            this.SyncToState();
        }

        public static void OverwritePrompt(string displayName)
        {
            int num = EditorUtility.DisplayDialogComplex(s_Content.m_OverwriteDialogHeader.text, s_Content.m_OverwriteDialogText.text, s_Content.m_OverwriteDialogButtonOK.text, s_Content.m_OverwriteDialogButtonCancel.text, s_Content.m_OverwriteDialogButtonVersion.text);
            if (num == 1)
            {
                UploadingBuildsUtility.RemoveUploadingBuild(displayName);
                AsyncProgressBar.Clear();
                s_UploadingBuildsMonitor.SyncToState();
            }
            else
            {
                UploadingBuildsUtility.ResumeBuildUpload(displayName, num == 0);
            }
        }

        private void SyncToState()
        {
            this.m_UploadingBuilds = UploadingBuildsUtility.GetUploadingBuilds();
        }

        private void Update()
        {
            if (!this.m_DidInit)
            {
                this.SyncToState();
                this.m_DidInit = true;
            }
            this.UploadSmoothing();
            if (this.m_UploadingBuilds.Length > 0)
            {
                this.UpdateBuild(ref this.m_UploadingBuilds[0]);
            }
            else
            {
                Deactivate();
            }
        }

        private void UpdateBuild(ref UploadingBuild build)
        {
            switch (build.status)
            {
                case UploadingBuildStatus.Authorizing:
                case UploadingBuildStatus.Authorized:
                    AsyncProgressBar.Display(s_Content.m_ProgressBarText.text, 0f);
                    break;

                case UploadingBuildStatus.Uploading:
                {
                    ValueSmoother progressSmoother = this.GetProgressSmoother(build.url);
                    progressSmoother.SetTargetValue(build.progress);
                    AsyncProgressBar.Display(s_Content.m_ProgressBarText.text, progressSmoother.GetSmoothValue());
                    break;
                }
                case UploadingBuildStatus.Uploaded:
                case UploadingBuildStatus.UploadFailed:
                    UploadingBuildsUtility.RemoveUploadingBuild(build.displayName);
                    AsyncProgressBar.Clear();
                    this.SyncToState();
                    break;

                default:
                    Debug.LogError("Unhandled enum");
                    break;
            }
        }

        private void UploadSmoothing()
        {
            if (this.m_ProgressSmoothers.Count > 0)
            {
                bool flag = false;
                List<string> list = null;
                foreach (KeyValuePair<string, ValueSmoother> pair in this.m_ProgressSmoothers)
                {
                    pair.Value.Update();
                    if (pair.Value.GetSmoothValue() < 1f)
                    {
                        flag = true;
                    }
                    else
                    {
                        if (list == null)
                        {
                            list = new List<string>();
                        }
                        list.Add(pair.Key);
                    }
                }
                if (list != null)
                {
                    foreach (string str in list)
                    {
                        this.m_ProgressSmoothers.Remove(str);
                    }
                }
                if (flag)
                {
                    this.SyncToState();
                }
            }
        }

        [CompilerGenerated]
        private sealed class <HandleNoSession>c__AnonStorey46
        {
            internal string displayName;

            internal void <>m__97(string errorMessage)
            {
                if (string.IsNullOrEmpty(errorMessage))
                {
                    UploadingBuildsUtility.ResumeBuildUpload(this.displayName);
                }
                else
                {
                    UploadingBuildsUtility.RemoveUploadingBuild(this.displayName);
                }
            }
        }

        private class Content
        {
            public GUIContent m_NoSessionDialogButtonOK = EditorGUIUtility.TextContent("UploadingBuildsMonitor.NoSessionDialogButtonOK");
            public GUIContent m_NoSessionDialogHeader = EditorGUIUtility.TextContent("UploadingBuildsMonitor.NoSessionDialogHeader");
            public GUIContent m_NoSessionDialogText = EditorGUIUtility.TextContent("UploadingBuildsMonitor.NoSessionDialogText");
            public GUIContent m_OverwriteDialogButtonCancel = EditorGUIUtility.TextContent("UploadingBuildsMonitor.OverwriteDialogButtonCancel");
            public GUIContent m_OverwriteDialogButtonOK = EditorGUIUtility.TextContent("UploadingBuildsMonitor.OverwriteDialogButtonOK");
            public GUIContent m_OverwriteDialogButtonVersion = EditorGUIUtility.TextContent("UploadingBuildsMonitor.OverwriteDialogButtonVersion");
            public GUIContent m_OverwriteDialogHeader = EditorGUIUtility.TextContent("UploadingBuildsMonitor.OverwriteDialogHeader");
            public GUIContent m_OverwriteDialogText = EditorGUIUtility.TextContent("UploadingBuildsMonitor.OverwriteDialogText");
            public GUIContent m_ProgressBarText = EditorGUIUtility.TextContent("UploadingBuildsMonitor.ProgressBarText");
        }
    }
}

